export function random(lower: number, upper: number): number {
    return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}

export function timerFormat(seconds: number | string, dateFormat = 'h:i:s'): string {
    seconds = Number(seconds)
    let obj: any = {}
    obj.h = Number.parseInt(String(seconds / 3600));
    obj.i = Number.parseInt(String((seconds - obj.h * 3600) / 60));
    obj.s = Number.parseInt(String(seconds - obj.h * 3600 - obj.i * 60));
    if (obj.h < 10) obj.h = '0' + obj.h;
    if (obj.i < 10) obj.i = '0' + obj.i;
    if (obj.s < 10) obj.s = '0' + obj.s;
    // 3.解析
    var rs = dateFormat.replace('h', obj.h).replace('i', obj.i).replace('s', obj.s);
    return rs;
}

export function toXY(node1: cc.Node, node2: cc.Node, rpos: cc.Vec2 = cc.v2(0, 0)) {
    const wpos = node1.convertToWorldSpaceAR(rpos)
    const pos = node2.convertToNodeSpaceAR(wpos)
    return pos
}

export function getNumFromStr(str: string) {
    const arr = str.match(/\d+/g).map(Number)
    return arr[0]
}

export function getDistance(start: cc.Vec2, end: cc.Vec2) {
    const pos = cc.v2(start.x - end.x, start.y - end.y);
    const dis = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
    return dis;
}

export function takeValueFromArray(arr: number[], value: number, max: number = 3) {
    let result = [];
    let count = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === value && count < max) {
            result.push(arr.splice(i, 1)[0]);
            count++;
            i--;
        }
    }
    return result;
}

export function getDiffArr(arr1: any[], arr2: any[]) {
    return arr1.filter(item => !arr2.includes(item))
}

export function countGameLevelData(arr: number[]) {
    let count = {};
    arr.forEach(num => {
        count[num] = (count[num] || 0) + 1;
    });
    return count
}